// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Player 2 Object Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 3 : TYPE_PLAYER2OBJECT

// Function declarations
reserve function Player2Object_Function61
reserve function Player2Object_Function62
reserve function Player2Object_Function63
reserve function Player2Object_Function64
reserve function Player2Object_Function65
reserve function Player2Object_Function66
reserve function Player2Object_Function67
reserve function Player2Object_Function68
reserve function Player2Object_Function69
reserve function Player2Object_Function70

// Static Values
public value PlayerObject_StateUp = 0;
public value PlayerObject_StateDown = 0;
public value PlayerObject_StateLeft = 0;
public value PlayerObject_StateRight = 0;
public value PlayerObject_StateJumpPress = 0;
public value PlayerObject_StateJumpHold = 0;
public value Player2Object_value13 = 0;
public value Player2Object_value14 = 0;

// Tables
public table Player2Object_table6
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table

public table Player2Object_table7
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table


function Player2Object_Function68
	temp0 = 0
end function


function Player2Object_Function69
	CheckNotEqual(object.value44, Player2Object_Function68)
	temp0 = checkResult
	CheckNotEqual(object.up, 1)
	temp0 &= checkResult
	if temp0 == 0
		CallFunction(PlayerObject_StartTailsFlight)
	end if
end function


function Player2Object_Function61
	if object.controlMode > -1
		PlayerObject_StateUp <<= 1
		PlayerObject_StateUp |= object[0].up
		PlayerObject_StateUp &= 0xFFFF
		PlayerObject_StateDown <<= 1
		PlayerObject_StateDown |= object[0].down
		PlayerObject_StateDown &= 0xFFFF
		PlayerObject_StateLeft <<= 1
		PlayerObject_StateLeft |= object[0].left
		PlayerObject_StateLeft &= 0xFFFF
		PlayerObject_StateRight <<= 1
		PlayerObject_StateRight |= object[0].right
		PlayerObject_StateRight &= 0xFFFF
		PlayerObject_StateJumpPress <<= 1
		PlayerObject_StateJumpPress |= object[0].jumpPress
		PlayerObject_StateJumpPress &= 0xFFFF
		PlayerObject_StateJumpHold <<= 1
		PlayerObject_StateJumpHold |= object[0].jumpHold
		PlayerObject_StateJumpHold &= 0xFFFF
		if object[0].state == PlayerObject_HandleFlyCarry
			PlayerObject_StateDown <<= 15
			PlayerObject_StateLeft <<= 15
			PlayerObject_StateRight <<= 15
			PlayerObject_StateJumpPress <<= 15
			PlayerObject_StateJumpHold <<= 15
		end if
		temp0 = PlayerObject_StateUp
		temp0 >>= 15
		object.up = temp0
		temp0 = PlayerObject_StateDown
		temp0 >>= 15
		object.down = temp0
		temp0 = PlayerObject_StateLeft
		temp0 >>= 15
		object.left = temp0
		temp0 = PlayerObject_StateRight
		temp0 >>= 15
		object.right = temp0
		temp0 = PlayerObject_StateJumpPress
		temp0 >>= 15
		object.jumpPress = temp0
		temp0 = PlayerObject_StateJumpHold
		temp0 >>= 15
		object.jumpHold = temp0
	else
		PlayerObject_StateUp = 0
		PlayerObject_StateDown = 0
		PlayerObject_StateLeft = 0
		PlayerObject_StateRight = 0
		PlayerObject_StateJumpPress = 0
		PlayerObject_StateJumpHold = 0
	end if
	if object[0].state != PlayerObject_Death
		if object[0].type != TypeName[Death Event]
			SetTableValue(object[0].xpos, Player2Object_value14, Player2Object_table6)
			SetTableValue(object[0].ypos, Player2Object_value14, Player2Object_table7)
			Player2Object_value14++
			Player2Object_value14 &= 15
			Player2Object_value13++
			Player2Object_value13 &= 15
			CheckEqual(object[0].gravity, GRAVITY_AIR)
			temp0 = checkResult
			CheckEqual(object[0].value42, 0)
			temp0 &= checkResult
			if temp0 == 0
				GetTableValue(object.value46, Player2Object_value13, Player2Object_table6)
				GetTableValue(object.value47, Player2Object_value13, Player2Object_table7)
			else
				object.value46 = object[0].xpos
				object.value47 = object[0].ypos
			end if
		else
			temp0 = Player2Object_value14
			temp0--
			if temp0 < 0
				temp0 += 16
			end if
			GetTableValue(object.value46, temp0, Player2Object_table6)
			GetTableValue(object.value47, temp0, Player2Object_table7)
		end if
	else
		temp0 = Player2Object_value14
		temp0--
		if temp0 < 0
			temp0 += 16
		end if
		GetTableValue(object.value46, temp0, Player2Object_table6)
		GetTableValue(object.value47, temp0, Player2Object_table7)
	end if
end function


function Player2Object_Function62
	CallFunction(Player2Object_Function61)
	if object[0].type == TypeName[Player Object]
		temp0 = object.angle
		temp0 += 16
		temp0 &= 224
		if temp0 == 0
			if object.left == 1
				temp0 = object[0].xpos
				temp0 -= 0x80000
				if object.xpos < temp0
					if object.xvel <= 0
						object.left = 0
					end if
				end if
			end if
			if object.right == 1
				temp0 = object[0].xpos
				temp0 += 0x80000
				if object.xpos > temp0
					if object.yvel <= 0
						object.right = 0
					end if
				end if
			end if
		end if
		if object[0].state != PlayerObject_HandleFlyCarry
			temp0 = object.value46
			temp1 = object[0].gravity
			temp1 |= object[0].value42
			if temp1 == 0
				if object[0].speed < 0x20000
					if object[0].speed > -0x20000
						if object[0].direction == FLIP_NONE
							temp0 -= 0x200000
						else
							temp0 += 0x200000
						end if
					end if
				end if
			end if
			temp0 -= object.xpos
			if temp0 != 0
				if temp0 < 0
					if temp0 <= -0x300000
						object.right = 0
						object.left = 1
					end if
					if object.speed != 0
						if object.direction == FLIP_X
							Cos256(temp1, object.angle)
							temp1 *= 192
							object.xpos -= temp1
						end if
					end if
				else
					if temp0 >= 0x300000
						object.left = 0
						object.right = 1
					end if
					if object.speed != 0
						if object.direction == FLIP_NONE
							Cos256(temp1, object.angle)
							temp1 *= 192
							object.xpos += temp1
						end if
					end if
				end if
			end if
			if object.animation == ANI_PUSHING
				object.value45++
				if object[0].direction == object.direction
					if object[0].animation == ANI_PUSHING
						object.value45 = 0
					end if
				end if
				if object.value45 >= 30
					if object.gravity == GRAVITY_GROUND
						CallFunction(PlayerObject_StartJump)
					end if
					object.value1 = 0
					object.value45 = 0
				end if
			else
				temp0 = object.ypos
				temp0 -= object.value47
				if temp0 > 0x200000
					object.value45++
					if object.value45 >= 64
						if object.gravity == GRAVITY_GROUND
							CallFunction(PlayerObject_StartJump)
						end if
						object.value1 = 0
						object.value45 = 0
					end if
				else
					object.value45 = 0
				end if
			end if
			if object.controlLock > 0
				if object.speed < 0x8000
					if object.speed > -0x8000
						object.value44 = Player2Object_Function63
					end if
				end if
			end if
		end if
	end if
end function


function Player2Object_Function63
	object.up = 0
	object.down = 0
	object.left = 0
	object.right = 0
	object.jumpPress = 0
	object.jumpHold = 0
	if object.controlLock == 0
		if object.gravity == GRAVITY_GROUND
			if object.speed < 0x4000
				if object.speed > -0x4000
					object.value44 = Player2Object_Function64
					object.value45 = 1
					if object.animation != ANI_SPINDASH
						if object.xpos < object.value46
							object.direction = FLIP_NONE
						else
							object.direction = FLIP_X
						end if
						object.down = 1
					end if
				end if
			end if
		end if
	end if
end function


function Player2Object_Function64
	if object.value45 < 64
		object.down = 1
		temp0 = object.value45
		temp0 &= 15
		if temp0 == 0
			object.jumpPress = 1
		else
			object.jumpPress = 0
		end if
		object.value45++
	else
		object.value45 = 0
		object.down = 0
		object.jumpPress = 0
		object.value44 = Player2Object_Function62
	end if
end function


function Player2Object_Function65
	CallFunction(Player2Object_Function61)
	if object.value25 == 0x3800
		object.animation = ANI_FLYING
	else
		object.animation = ANI_SWIMMING
	end if
	temp0 = object.value46
	temp0 -= object.xpos
	temp1 = temp0
	temp0 >>= 4
	if object.xpos < object.value46
		object.direction = FLIP_NONE
		if temp0 > 0xC0000
			temp0 = 0xC0000
		end if
		if object[0].xvel > 0
			temp0 += object[0].xvel
		end if
		temp0 += 0x10000
		if temp0 > temp1
			temp0 = temp1
			temp1 = 0
		end if
	else
		object.direction = FLIP_X
		if temp0 < -0xC0000
			temp0 = -0xC0000
		end if
		if object[0].xvel < 0
			temp0 += object[0].xvel
		end if
		temp0 -= 0x10000
		if temp0 < temp1
			temp0 = temp1
			temp1 = 0
		end if
	end if
	object.xpos += temp0
	if object.ypos < object.value47
		object.ypos += 0x10000
	end if
	if object.ypos > object.value47
		object.ypos -= 0x10000
	end if
	if object[0].type != TypeName[Death Event]
		if object[0].state != PlayerObject_Death
			if object[0].state != PlayerObject_Drown
				if object[0].state != PlayerObject_HandleRollDecel
					if temp1 == 0
						temp0 = object.value47
						temp0 -= object.ypos
						if temp0 < 0
							FlipSign(temp0)
						end if
						if temp0 < 0x20000
							object.state = PlayerObject_HandleAir
							object.animation = ANI_JUMPING
							object.gravity = GRAVITY_AIR
							object.tileCollisions = 1
							object.interaction = 1
							object.controlMode = 1
							object.controlLock = 0
							object.angle = 0
							object.value44 = Player2Object_Function62
							object.collisionPlane = object[0].collisionPlane
							PlayerObject_StateUp = 0
							PlayerObject_StateDown = 0
							PlayerObject_StateLeft = 0
							PlayerObject_StateRight = 0
							PlayerObject_StateJumpPress = 0
							PlayerObject_StateJumpHold = 0
						end if
					end if
				end if
			end if
		end if
	end if
end function


function Player2Object_Function66
	if object[0].type == TypeName[Player Object]
		if object.outOfBounds == 1
			object.value43++
		else
			object.value43 = 0
		end if
		if object.value43 > 239
			object.value43 = 0
			object.state = Player2Object_Function65
			object.xpos = object[0].xpos
			object.ypos = screen.yoffset
			object.ypos -= 128
			object.ypos <<= 16
			object.xvel = 0
			object.yvel = 0
			object.speed = 0
			object.tileCollisions = 0
			object.interaction = 0
			object.controlMode = 1
			object.value18 = 4
			object.value3 = 0
			object.value4 = 0
		end if
		CheckEqual(object[0].state, PlayerObject_Death)
		temp0 = checkResult
		CheckEqual(object[0].type, TypeName[Death Event])
		temp0 |= checkResult
		CheckNotEqual(object.state, PlayerObject_Knockback)
		temp0 &= checkResult
		CheckNotEqual(object.state, PlayerObject_Death)
		temp0 &= checkResult
		if temp0 == 1
			object.value43 = 0
			object.state = Player2Object_Function65
			object.xvel = 0
			object.yvel = 0
			object.speed = 0
			object.tileCollisions = 0
			object.interaction = 0
		end if
	end if
end function


function Player2Object_Function67
	if object.state == PlayerObject_Death
		object.value44 = Player2Object_Function68
	end if
	if object.state == PlayerObject_Drown
		object.value44 = Player2Object_Function68
	end if
	CallFunction(object.value44)
	if object.state != PlayerObject_Knockback
		if object.value8 > 0
			object.value8--
			GetBit(temp0, object.value8, 2)
			if temp0 == 1
				object.visible = 0
			else
				object.visible = 1
			end if
		end if
	end if
	if object.value7 > 0
		if object.state != PlayerObject_Knockback
			if object.value7 > 0x7D0
				object.value7 = 120
				object.value8 = 3
			end if
		end if
		object.value7--
		if object.value7 == 0
			object.value8 = 0
			object.visible = 1
		end if
	end if
	if object.state != PlayerObject_LookingUp
		if object.state != PlayerObject_Crouching
			if object.lookPosY > 0
				object.lookPosY -= 2
			end if
			if object.lookPosY < 0
				object.lookPosY += 2
			end if
		end if
	end if
	if object.state != PlayerObject_HandleTailsFlight
		if object.value26 != 0
			StopSfx(SfxName[Flying])
			StopSfx(SfxName[Tired])
			object.value26 = 0
		end if
	end if
end function


function Player2Object_Function70
	CallNativeFunction2(ReceiveEntity, object.entityPos, 1)
	object.type = TypeName[Player 2 Object]
	if object.value6 > 0
		object.value6--
		if object.value6 < 1
			currentPlayer = object.entityPos
			CallFunction(PlayerObject_SwitchPhysicsState)
			temp0 = 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 += object[currentPlayer].value6
			next
			if temp0 == 0
				if SlowDownMusic != 0
					CallFunction(SlowDownMusic)
				end if
			end if
			object.value6 = 0
		end if
	end if
	if object.state != PlayerObject_Knockback
		if object.value8 > 0
			object.value8--
			GetBit(temp0, object.value8, 2)
			if temp0 == 1
				object.visible = 0
			else
				object.visible = 1
			end if
		end if
	end if
	if object.value7 > 0
		object.value7--
		if object.value7 == 0
			temp0 = 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 += object[currentPlayer].value7
			next
			if temp0 == 0
				if music.currentTrack == 2
					PlayMusic(0)
				end if
			end if
			if object[+playerCount].type == invincibilityType
				currentPlayer = object.entityPos
				arrayPos0 = currentPlayer
				arrayPos0 += playerCount
				CallFunction(PlayerObject_RestorePowerup)
			end if
		end if
	end if
	if object.state != PlayerObject_LookingUp
		if object.state != PlayerObject_Crouching
			if object.lookPosY > 0
				object.lookPosY -= 2
			end if
			if object.lookPosY < 0
				object.lookPosY += 2
			end if
		end if
	end if
	if object.value11 > 0
		object.value11--
		if object.value11 == 0
			if object.entityPos == screen.cameraTarget
				screen.cameraStyle = 0
			end if
		end if
	end if
	if object.state != PlayerObject_HandleTailsFlight
		if object.value26 != 0
			StopSfx(SfxName[Flying])
			StopSfx(SfxName[Tired])
			object.value26 = 0
		end if
	end if
end function


event ObjectMain
	if options.vsMode == 0
		CallFunction(Player2Object_Function67)
		CallFunction(object.state)
		ProcessAnimation()
		CallFunction(TailsObject_Function60)
	else
		CallFunction(Player2Object_Function70)
		CallFunction(object.state)
		ProcessAnimation()
		if object.propertyValue == 1
			CallFunction(TailsObject_Function60)
		end if
	end if
	if object.entityPos == screen.cameraTarget
		if object.animation == ANI_JUMPING
			screen.adjustCameraY = object.value30
		else
			if screen.adjustCameraY == object.value30
				screen.adjustCameraY = 0
				object.iypos += object.value30
			end if
		end if
	end if
	if object.value34 == 0
		temp0 = object.value42
		object.value42 = object.gravity
		ProcessObjectMovement()
		object.value42 ^= 1
		CheckEqual(object.gravity, GRAVITY_GROUND)
		object.value42 |= checkResult
		object.value42 ^= 1
		if temp0 == 1
			if object.value42 == 0
				object.value19 = 0
				if object.animation == ANI_JUMPING
					if object.down == 0
						if object.state != PlayerObject_HandleRolling
							if object.state != PlayerObject_HandleRollDecel
								object.animation = ANI_WALKING
								if object.entityPos == screen.cameraTarget
									screen.adjustCameraY = 0
								end if
								object.iypos += object.value30
							end if
						end if
					end if
				end if
			end if
		end if
	else
		object.value34 = 0
	end if
	if options.vsMode == 0
		CallFunction(Player2Object_Function66)
	end if
end event


event ObjectDraw
	if object.animation != object.prevAnimation
		object.prevAnimation = object.animation
		object.frame = 0
		object.animationTimer = 0
		object.animationSpeed = 0
	end if
	if object.propertyValue == 1
		if object.value13 != object.animation
			if object.animation > 4
				object.value12 = 0
			end if
			if object.value13 > 4
				object.value12 = 0
			end if
			object.value13 = object.animation
		end if
		switch object.value13
		case 0
			if object.frame == 0
				temp0 = object.value12
				temp0 >>= 3
				if object.visible == 1
					DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
				end if
			end if
			break
		case 1
		case 3
		case 4
		case 21
		case 36
		case 37
			temp0 = object.value12
			temp0 >>= 3
			if object.visible == 1
				DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			end if
			break
		case 7
		case 9
			temp0 = object.value12
			temp0 >>= 2
			temp0 += 11
			if object.visible == 1
				DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			end if
			break
		case 10
		case 43
			temp0 = object.value12
			temp0 >>= 2
			CheckEqual(object.xvel, 0)
			temp1 = checkResult
			CheckEqual(object.yvel, 0)
			temp1 &= checkResult
			temp2 = object.rotation
			if temp1 == 0
				ATan2(object.rotation, object.xvel, object.yvel)
				object.rotation += 16
				object.rotation &= 255
				object.rotation >>= 5
				switch object.rotation
				case 0
				case 8
					temp0 += 5
					object.rotation = 0
					break
				case 1
					temp0 += 8
					if object.direction == FLIP_NONE
						object.rotation = 64
					else
						object.rotation = 0
					end if
					break
				case 2
					temp0 += 5
					object.rotation = 64
					break
				case 3
					temp0 += 8
					if object.direction == FLIP_NONE
						object.rotation = 128
					else
						object.rotation = 64
					end if
					break
				case 4
					temp0 += 5
					object.rotation = 128
					break
				case 5
					temp0 += 8
					if object.direction == FLIP_NONE
						object.rotation = 192
					else
						object.rotation = 128
					end if
					break
				case 6
					temp0 += 5
					object.rotation = 192
					break
				case 7
					temp0 += 8
					if object.direction == FLIP_NONE
						object.rotation = 0
					else
						object.rotation = 192
					end if
					break
				end switch
				if object.direction == FLIP_X
					object.rotation += 128
				end if
			else
				temp0 += 5
				object.rotation = 0
			end if
			object.rotation <<= 1
			if object.visible == 1
				DrawSpriteFX(temp0, FX_ROTATE, object.xpos, object.ypos)
			end if
			object.rotation = temp2
			break
		case 17
			temp0 = object.value12
			temp0 /= 10
			temp0 += 11
			if object.visible == 1
				DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			end if
			break
		end switch
	end if
	DrawObjectAnimation()
end event


event ObjectStartup
	Player2Object_value13 = 1
	Player2Object_value14 = 0
	PlayerObject_StateUp = 0
	PlayerObject_StateDown = 0
	PlayerObject_StateLeft = 0
	PlayerObject_StateRight = 0
	PlayerObject_StateJumpPress = 0
	PlayerObject_StateJumpHold = 0
	temp7 = 0
	if options.vsMode == 1
		ResetObjectEntity(1, TypeName[Player 2 Object], 1, object[0].xpos, object[0].ypos)
		vs.restartX1 = object[0].xpos
		vs.restartY1 = object[0].ypos
		vs.restartX2 = object[0].xpos
		vs.restartY2 = object[0].ypos
		object[1].groupID = GROUP_PLAYERS
		object[1].state = PlayerObject_HandleAir
		object[1].priority = PRIORITY_ACTIVE
		object[1].drawOrder = -1
		object[1].value18 = 4
		object[1].value31 = 0x2000
		object[1].value33 = PlayerObject_StartSpindash
		currentPlayer = 1
		CallFunction(PlayerObject_SwitchPhysicsState)
		object[1].value38 = HITBOX_AUTO
		object[1].value39 = HITBOX_AUTO
		object[1].value40 = HITBOX_AUTO
		object[1].value41 = HITBOX_AUTO
		switch vs.player2Type
		case 0
			LoadAnimation("Sonic.ani")
			object[1].propertyValue = 0
			object[1].value30 = -5
			object[1].value32 = PlayerObject_ShieldAbilities
			ANI_PEELOUT = ANI_RUNNING
			break
		case 1
			LoadAnimation("Tails.ani")
			object[1].propertyValue = 1
			object[1].value30 = -1
			object[1].value32 = PlayerObject_StartTailsFlight
			stage.player2Enabled = 0
			break
		case 2
			LoadAnimation("Knuckles.ani")
			object[1].propertyValue = 2
			object[1].value30 = -5
			object[1].value32 = PlayerObject_StartKnuxGlide
			ANI_PEELOUT = ANI_RUNNING
			break
		end switch
		stage.player2Enabled = 0
		playerCount = 2
		temp7 = 1
	end if
	if stage.player2Enabled == 1
		ResetObjectEntity(1, TypeName[Player 2 Object], 1, object[0].xpos, object[0].ypos)
		if starPostID == 0
			object[1].xpos -= 0x100000
		end if
		LoadAnimation("Tails.ani")
		object[1].groupID = GROUP_PLAYERS
		object[1].propertyValue = 1
		object[1].state = PlayerObject_HandleAir
		object[1].priority = PRIORITY_ACTIVE
		object[1].controlMode = 0
		object[1].drawOrder = -1
		object[1].value18 = 4
		currentPlayer = 1
		CallFunction(PlayerObject_SwitchPhysicsState)
		object[1].value31 = 0x2000
		object[1].value30 = -1
		object[1].value16 = 1
		object[1].value32 = Player2Object_Function69
		object[1].value33 = PlayerObject_StartSpindash
		CheckCurrentStageFolder("Continue")
		if checkResult == 1
			object[1].value44 = Player2Object_Function68
		else
			object[1].value44 = Player2Object_Function62
		end if
		temp0 = 0
		while temp0 < 16
			SetTableValue(object[0].xpos, temp0, Player2Object_table6)
			SetTableValue(object[0].ypos, temp0, Player2Object_table7)
			temp0++
		loop
		object[1].value38 = HITBOX_AUTO
		object[1].value39 = HITBOX_AUTO
		object[1].value40 = HITBOX_AUTO
		object[1].value41 = HITBOX_AUTO
		temp7 = 1
	end if
	if temp7 == 1
		LoadSpriteSheet("Players/Tails1.gif")
		SpriteFrame(-22, -8, 16, 24, 82, 199)
		SpriteFrame(-26, -8, 20, 24, 99, 199)
		SpriteFrame(-26, -8, 20, 24, 120, 199)
		SpriteFrame(-26, -8, 20, 24, 141, 199)
		SpriteFrame(-26, -8, 20, 24, 162, 199)
		SpriteFrame(-35, -8, 24, 16, 231, 166)
		SpriteFrame(-35, -8, 24, 16, 231, 183)
		SpriteFrame(-35, -8, 24, 16, 231, 200)
		SpriteFrame(-25, 9, 20, 16, 235, 217)
		SpriteFrame(-25, 9, 18, 16, 237, 234)
		SpriteFrame(-25, 9, 20, 16, 216, 234)
		SpriteFrame(-30, -6, 24, 16, 231, 166)
		SpriteFrame(-30, -6, 24, 16, 231, 183)
		SpriteFrame(-30, -6, 24, 16, 231, 200)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
